#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

int i = 0;
int thread_sig = 0;


void *thread_write(void *arg)
{
    while(i < 100) {
        if (thread_sig == 0) {
            i++;
            thread_sig = 1;
            sleep(1);
        }
    }

    return NULL;
}

void *thread_read(void *arg)
{
    while(i < 100){
        if (thread_sig == 1) {
            printf("i = %d\n", i);
            thread_sig = 0;
            sleep(1);
        }
    }

    return NULL;
}

int main(int argc, char *argv[])
{
    
    pthread_t tid1;
    pthread_t tid2;
    int pc_1;
    int pc_2;
    
    pc_1 = pthread_create(&tid1, NULL, thread_read, NULL);
    pc_2 = pthread_create(&tid2, NULL, thread_write, NULL);

    if (pc_1 != 0) {
        printf("pc_1: %s\n", strerror(pc_1));
        return -1;
    }

    if (pc_2 != 0) {
        strerror(pc_2);
        return -1;
    }

    sleep(20);

    return 0;
}
